/*

  The MIT License

  Copyright (c) 2008 - 2011. Dimarik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

package ru.goodcore.core
{

import flash.utils.IDataInput;
import flash.utils.IDataOutput;
import flash.utils.IExternalizable;

/**
 *  @author				Dimarik
 *  @version			1.1
 *  @langversion		3.0
 *  @playerversion		9.0
 * 
 */
public class ProductInfo implements IExternalizable {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	public function ProductInfo() {
		super();
	}

	//-------------------------------------------------------------------------
	//
	//   Properties
	//
	//-------------------------------------------------------------------------

	//---------------------------------
	//   generator
	//---------------------------------
	private var _generator:	String;

	public function get generator():String {
		if ( !this._generator )
			this._generator = this.manufacturer + ' ' + [this._major, this._minor, this._buildNumber].join('.');
		return this._generator;
	}

	//---------------------------------
	//   productId
	//---------------------------------
	private var _productId:uint;

	public function get productId():uint {
		return this._productId;
	}

	//---------------------------------
	//   manufacturer
	//---------------------------------
	private var _manufacturer:String;

	public function get manufacturer():String {
		if (this._manufacturer === null) {
			switch (this._productId) {
				case 3:
					this._manufacturer = "Аdobe Flex";
					break;

				default:
					this._manufacturer = 'Unknown';
			}
		}
		return this._manufacturer;
	}

	//---------------------------------
	//   compileTime
	//---------------------------------
	private var _compileTime:Date;

	public function get compileTime():Date {
		return this._compileTime;
	}

	//---------------------------------
	//   edition
	//---------------------------------
	private var _edition:uint;

	public function get edition():uint {
		return this._edition;
	}

	//---------------------------------
	//   major
	//---------------------------------
	private var _major:	uint;

	public function get major():uint {
		return this._major;
	}

	//---------------------------------
	//   minor
	//---------------------------------
	private var _minor:	uint;

	public function get minor():uint {
		return this._minor;
	}

	//---------------------------------
	//   buildNumber
	//---------------------------------
	private var _buildNumber:	uint;

	public function get buildNumber():uint {
		return this._buildNumber;
	}

	//-------------------------------------------------------------------------
	//
	//   Interface implementation: IExternalizable
	//
	//-------------------------------------------------------------------------

	public function readExternal(input:IDataInput):void {
		this.clear();

		this._productId		= input.readUnsignedInt();
		this._edition		= input.readUnsignedInt();
		this._major			= input.readByte();
		this._minor			= input.readByte();
		this._buildNumber	= this.readUnsignedInt64(input);

		var time:Number = this.readUnsignedInt64(input);
		this._compileTime = new Date(time);
	}

	public function writeExternal(output:IDataOutput):void {
		void;
	}

	public function clear():void {
		this._manufacturer = null;
		this._productId	= 0;
		this._edition = 0;
		this._major = 0;
		this._minor = 0;
		this._buildNumber = 0;
	}

	private function readUnsignedInt64(input:IDataInput):Number {
		var n1:Number = input.readUnsignedInt();
		var n2:Number = input.readUnsignedInt();
		return n2 * 0x100000000 + n1;	
	}

	public function toString():String {
		var result:String = 
			'[ProductInfo; generator: ' + this.manufacturer + ' ' + [this._major, this._minor, this._buildNumber].join('.') + 
			(this._compileTime ? ', compileTime: ' + this._compileTime.toString() : '') + 
			']';
		return result;
	}

}
}